/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package my.javax.microedition.contactless.ndef;

/**
 *
 * @author emarkki
 */
public class NDEFRecordType {

    public static final int EMPTY = 0;
    public static final int EXTERNAL_RTD = 4;
    public static final int MIME = 1;
    public static final int NFC_FORUM_RTD = 3;
    public static final int UNKNOWN = 5;
    public static final int URI = 2;

    private int type;
    private String name;

    private static final String[] prefixes = new String[] {
        "",
        "",
        "",
        "urn:nfc:wkt:",
        "urn:nfc:ext:",
        ""
    };

    public NDEFRecordType(int type, String name) {
        this.type = type;
        if(type >= prefixes.length || type < 0) {
            throw new IllegalArgumentException("Unknown type: " + type);
        }
        if(!name.startsWith(prefixes[type])) {
            throw new IllegalArgumentException("Type " + type + " should start with " + prefixes[type]);
        }
        this.name = name.substring(prefixes[type].length());
    }

    public String getName() {
        return name;
    }

    /** Not standard, TODO: hide this method */
    public int getType() {
        return type;
    }

    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final NDEFRecordType other = (NDEFRecordType) obj;
        if (this.type != other.type) {
            return false;
        }
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    public int hashCode() {
        int hash = 5;
        hash = 11 * hash + this.type;
        hash = 11 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    public String toString() {
        return "RecordType["+getType()+","+getName()+"]";
    }
}
